/*******************************************************************************
 * Copyright (c) 2020 fortiss GmbH
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Jose Cabral - initial implementation
 *******************************************************************************/

#pragma once

#include "forte/datatypes/forte_string.h"
#include <string>

/**
 * Parse the configuration file for the mqtt client. It supports the following keys:
 *   endpoint (string)
 *   username (string)
 *   password (string)
 *
 *   The parser looks for an endpoint, and only after it matches the one passed as argument, it starts storing the
 * following information, and it will keep reading after another endpoint other the end of file is found
 */

namespace forte::com_infra::mqtt_paho {

  class CMQTTClientConfigFileParser {
    public:
      /**
       * Encapsulation for the result used by the class when parsing the configuration file
       */
      class MQTTConfigFromFile {
        public:
          MQTTConfigFromFile(std::string &paUsername, std::string &paPassword) :
              mUsername(paUsername),
              mPassword(paPassword) {
          }
          std::string &mUsername;
          std::string &mPassword;
      };

      /**
       * Reads a configuration file, parse it and store the data
       * @param paFileLocation Path to file to be parsed
       * @param paEndpoint Endpoint of the client to be looked for
       * @param paResult Place to store the results
       * @return True if no error occurred, false otherwise
       */
      static bool
      loadConfig(const std::string &paFileLocation, const std::string &paEndpoint, MQTTConfigFromFile &paResult);

      explicit CMQTTClientConfigFileParser() = delete;
      virtual ~CMQTTClientConfigFileParser() = delete;

    private:
      /**
       * Allowed key in the configuration file for the mqtt clients
       */
      enum MQTTConfigKeys {
        eEndoint, /**< eEndoint */
        eUsername, /**< eUsername */
        ePassword, /**< ePassword */
        eUnknown /**< eUnknown */
      };

      /**
       * Strings for the allowed keys in the configuration file for the mqtt clients
       */
      static const char *const mConfigKeysNames[eUnknown];
  };

} // namespace forte::com_infra::mqtt_paho
